;thread for tapping screws type f
;outputs; [body]
;
;simplified thread?
{If:[sel_th]=Simplified}
	_Polyline _PersistentClose=_No 0,[dminth]/2,-([pth]/2) _Mode=_Line 0,[d]/2,-(([d]-[dminth])/2)-([pth]/2) 0,[d]/2,[tipend] 0,[typef_dtip]/2,-[l] 0,0,-[l] _Enter
	_SelLast
	{Revolve:0,0,0:0,0,[l]}
	{Set:[thread]={SelectedObjs}}
	{SetName:[st]}
	_SelNone
{EndIf:}
;
;detailed thread?
{If:[sel_th]=Detailed}
	;
	;main thread spiral
	{Set:[turns]={Math:(abs([tipend]-[tipstart])/[pth])-0.25}}
	_Spiral 0,0,[tipend] 0,0,[tipend]+(([turns]-1)*[pth]) _Mode=_Turns _Turns=[turns]-1 0,[d]/2,[tipend] 0,[d]/2,[tipend]+(([turns]-1)*[pth]) _Enter
	_SelLast
	{Set:[spiral]={SelectedObjs}}	
	_SelNone
	;
	;thread section
	{Set:[SectionID]={Guid}}
	_Polyline _PersistentClose=_No 0,([d]/2)-([pth]*0.6),[tipend] _Mode=_Line 0,([d]/2)-([pth]*0.6),[tipend]+([cth]/2)+(tan(30d)*([pth]*0.6)) 0,[d]/2,[tipend]+([cth]/2) 0,[d]/2,[tipend]-([cth]/2) 0,[d]/2,[tipend]+([cth]/2) 0,([d]/2)-([pth]*0.6),[tipend]-([cth]/2)-(tan(30d)*([pth]*0.6)) _Close
	_SelLast
	{Set:[section]={SelectedObjs}}
	{SetName:[SectionID]}
	{Duplicate:[section]}
	_Rotate _Copy=_No 0,0 {ObjectSnap:[spiral]:StartXY} {ObjectSnap:[spiral]:EndXY}
	_Move 0,0,{ObjectSnap:[spiral]:StartZ} 0,0,{ObjectSnap:[spiral]:EndZ}
	;
	;thread end near the head
	_Spiral 0,0,[tipend]+(([turns]-1)*[pth]) 0,0,[tipend]+([turns]*[pth]) _Mode=_Turns _Turns=1 0,[d]/2,[tipend]+(([turns]-1)*[pth]) 0,([dminth]/2)-[tol],[tipend]+([turns]*[pth]) _Enter
	_SelLast
	{Set:[spiral2]={SelectedObjs}}
	_Rotate _Copy=_No 0,0 {ObjectSnap:[spiral2]:StartXY} {ObjectSnap:[spiral]:EndXY}
	_SelNone
	;
	_SelId [section]
	{Duplicate:[section]}
	_Move 0,[d]/2,{ObjectSnap:[spiral]:StartZ} 0,([dminth]/2)-[tol],{ObjectSnap:[spiral2]:EndZ}
	_Rotate _Copy=_No 0,0 {ObjectSnap:[spiral]:StartXY} {ObjectSnap:[spiral2]:EndXY}
	_SelNone
	;
	;thread end at tip
	_Spiral 0,0,[tipend] 0,0,[tipend]-([pth]*1.5) _Mode=_Turns _Turns=1.5 0,[d]/2,[tipend] 0,([dminth]/2)-(tan(3d)*([pth]*1.5))-[tol],[tipend]-([pth]*1.5) _Enter
	_SelLast
	{Set:[spiral3]={SelectedObjs}}
	_SelNone
	;
	_SelId [section]
	_Copy {ObjectSnap:[spiral]:Start} {ObjectSnap:[spiral3]:End} _Enter
	_SelNone
	_SelLast
	_Rotate3D 0,-([dminth]/2)+(tan(3d)*([pth]*1.5))+[tol],[tipend]-([pth]*1.5) 0,-([dminth]/2)+(tan(3d)*([pth]*1.5))+[tol],0 _Copy=_No 180d _Enter
	;
	_SelId [spiral]
	_SelId [spiral2]
	_SelId [spiral3]
	_Join
	{Set:[spiral]={SelectedObjs}}
	_SelNone
	;
	;sweep and join to the body
	-_Sweep1 '_SelID [spiral] '-_SelName [SectionID] _Enter _Natural _Enter _Style=_Roadlike 0,0,0 0,0,[tipend] _Simplify=_None _Closed=_No _ShapeBlending=_Local _Enter
	_SelLast
	{Cap:[tol]}
	'_SelID [body]
	{BooleanUnion:[tol]}
	{Set:[body]={SelectedObjs}}
	_SelNone
	;
	;delete the sweep curves
	_SelID [spiral]
	_Delete
	-_SelName [SectionID]
	_Delete
{EndIf:}